package com.ms.oschina.adapter;

import java.util.List;
import java.util.concurrent.ExecutionException;

import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.base.SuperBaseAdapter;
import com.ms.oschina.bean.News;
import com.ms.oschina.bean.NewsList;
import com.ms.oschina.viewholder.SynthesizeViewHolder;

/**
 * 具有加载更多功能，自动计算pageindex。
 * @author Administrator
 *
 * @param <T>
 */
public abstract class PageIndexListAdapter<T> extends SuperBaseAdapter<T> {

	protected int pageIndex = 0;
	private int mark;

	public PageIndexListAdapter(AbsListView listView, List<T> list,
			boolean loadMore) {
		super(listView, list, loadMore);
	}

	@Override
	public BaseViewHolder<T> getViewHolder(int position, T e) {
		return getItemViewHolder();
	}

	public abstract BaseViewHolder<T> getItemViewHolder();

	@Override
	public List<T> loadMore() throws Exception {
		pageIndex++;
		return getLoadMoreData();
	}

	public abstract List<T> getLoadMoreData() throws Exception;

	@Override
	public void loadMoreFailure() {
		if (pageIndex != 1) {
			pageIndex--;
		}
	}

	public void initPageIndex() {
		pageIndex = 1;
	}

	/**
	 * 添加标记 根据标记区分哪个页面点击
	 * 
	 * @param mark
	 */
	public void setMark(int mark) {
		this.mark = mark;
	}

	public int getMark() {
		return mark;
	}

	@Override
	public int getCurrentPage() {
		return pageIndex;
	}

}
